package com.liang.auth.application.controller;

import cn.hutool.core.collection.CollUtil;
import com.google.common.base.Preconditions;
import com.liang.auth.application.convert.AuthRolePermissionDTOConverter;
import com.liang.auth.application.dto.AuthRolePermissionDTO;
import com.liang.auth.api.entity.Result;
import com.liang.auth.domain.bo.AuthRolePermissionBO;
import com.liang.auth.domain.service.AuthRolePermissionDomainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/rolePermission")
@RequiredArgsConstructor
public class RolePermissionController {

    private final AuthRolePermissionDomainService authRolePermissionDomainService;

    /**
     * 新增角色权限关联关系
     */
    @PostMapping("/save")
    public Result<Void> save(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        log.info("RolePermissionController.save.dto：{}", authRolePermissionDTO);
        try {
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(), "角色ID不能为空");
//            Preconditions.checkNotNull(authRolePermissionDTO.getPermissionId(), "权限ID不能为空");
            Preconditions.checkArgument(CollUtil.isNotEmpty(authRolePermissionDTO.getPermissionIdList()), "角色关联权限列表不能为空");
            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConverter.INSTANCE.convertDTO2BO(authRolePermissionDTO);
            authRolePermissionDomainService.save(authRolePermissionBO);
            return Result.ok("新增角色权限关联关系成功");
        } catch (Exception e) {
            log.error("RolePermissionController.save.error：{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
}
